<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class C_User extends CI_Controller {

 function __construct()
 {
	parent::__construct();

    $this->load->model('m_booking', '', true);
	$this->load->model('m_user', '', true);
	
	$this->table	= 'users';
 }
 function get_list($offset = ''){
	/* pagination */
	$selects = "*";
	$joins = FALSE;
	
	$selects	= "		users.*,
						 user_groups.group_name 
					  ";
	$joins		= array(
							'user_groups'		=> array('users.gro_id		= user_groups.id_group', 'LEFT')
					   );	
						   
	$wheres = FALSE;
	$likes = FALSE;
	
	$limit = FALSE;
	$total = $this->m_booking->count_rows_join_by_filter('users', $selects, $joins, $wheres, $likes = FALSE, $between = FALSE, $groupby = FALSE, $options = FALSE);
	
	$data['table'] = $this->m_booking->join_by_filter('users', $selects, $joins, $wheres, $likes, '', 'uid', 'DESC', '', $limit, $offset);
	
	$config['base_url'] = base_url().'admin/c_users/index/';
	$config['total_rows'] = $total;
	$config['uri_segment'] = 4;
	$config['per_page'] = $limit;
	
	$this->pagination->initialize($config);
	
	$data['pag_links'] = $this->pagination->create_links();

	$data['title'] = 'Pagination';
	
	$data['showPage'] = 'admin/c_user/get_list';
	$data['body']='v_user/v_user_list';
	$this->load->view('admin/template',$data);
 }
 /**
	 * get List
	 */	
function get_list_group($offset = '')
{	
	/* pagination */
	$selects = "*";
	$joins = FALSE;
	
	$selects	= "		* ";
	$joins		= FALSE;
						   
	$wheres = FALSE;
	$likes = FALSE;
	
	$limit = 20;
	$total = $this->m_booking->count_rows_join_by_filter('user_groups', $selects, $joins, $wheres, $likes = FALSE, $between = FALSE, $groupby = FALSE, $options = FALSE);
	
	$data['table'] = $this->m_booking->join_by_filter('user_groups', $selects, $joins, $wheres, $likes, '', 'id_group', 'DESC', '', $limit, $offset);
	
	$config['base_url'] = base_url().'admin/c_users/get_list_group/';
	$config['total_rows'] = $total;
	$config['uri_segment'] = 4;
	$config['per_page'] = $limit;
	
	$this->pagination->initialize($config);
	
	$data['pag_links'] = $this->pagination->create_links();

	$data['title'] = 'Pagination';
	
	$data['showPage'] = 'admin/c_user/get_list_group';
	$data['body']='v_user/v_users_groups_list';
	$this->load->view('admin/template',$data);
}
 
 
 /*
 show page new
 */
 function page_new(){
	$where = array('status' => 1);;
	$data['user_groups']	=	$this->m_booking->get_select_by_fields('user_groups',$where,'*','id_group');
	
	$data['showPage'] = 'admin/c_user/get_list';
	$data['body']='v_user/v_user_new';
	$this->load->view('admin/template',$data);
 }
 /*
 Insert data to DB
 */
 function insert ($type = FALSE){ 
	$gro_id = $this->m_booking->encrypt_decrypt('decrypt', $this->input->post('gro_id'));
	$uuid = uniqid('', true);
	$hash = $this->m_user->hashSSHA($this->input->post('password'));
	$encrypted_password = $hash["encrypted"]; // encrypted password
	$salt = $hash["salt"]; // salt
	
	$data_arr = array(
						'gro_id'		=> $gro_id, 
	 					'unique_id'		=> $uuid, 
						'name'			=> $this->input->post('user_name'), 
						'email' 		=> $this->input->post('email_address'), 
						'encrypted_password' 	=> $encrypted_password, 
						'salt' 			=> $salt, 
	 				    'created_date' 	=> date('Y-m-d H:i:s')  
					 );
	 $data['table'] = $result =	$this->m_booking->insert('users', $data_arr, '');
	 if($result){
		  $id = $this->db->insert_id();									
		  // Get List Permission User By ID	
		  $wheres_con	= array('id_group' => $gro_id);
		  $controller	= $this->m_booking->get_select_by_fields('user_groups', $wheres_con, 'controller_id', 'controller_id');
		  
		  // Insert (Set Controller Permission)
		  $data_permission	= array(								
									  'user_id'		=> $id,
									  'controller_id'	=> $controller[0]['controller_id'],
									  );
		  if(! $this->m_booking->insert('controllers_permission', $data_permission))
		  {
			  $error++;
			  $error_msg	.= '<p>-<em>[#copy group permission]</em><p/>';					
		  }	
	 }else{
	 }
	 
	 if($type == 'customer'){
	 	redirect(base_url().'c_customer_home/page_new_customer_info/'.$id, 'location', 302);
	 }else{
	 	redirect(base_url().'admin/c_user/get_list', 'location', 302);
	 } 
 }
 /*
 page edit
 */
 function page_edit($id = false){
	 $id = $this->m_booking->encrypt_decrypt('decrypt', $id);
	 $where_group = array('status' => 1);;
	 $data['user_groups']	=	$this->m_booking->get_select_by_fields('user_groups',$where_group,'*','id_group');
	 $where = array('uid' => $id);
	 $data['table']	=	array_shift($this->m_booking->get_by_fields('users',$where,'uid'));
	 
	 $data['showPage'] = 'admin/c_user/get_list';
	 $data['body']='v_user/v_user_edit';
	 $this->load->view('admin/template',$data);
 }
 
 /*
 page update data
 */
 function update($id = false){
	$id = $this->m_booking->encrypt_decrypt('decrypt', $id);
	$gro_id = $this->m_booking->encrypt_decrypt('decrypt', $this->input->post('gro_id'));
	
	// Check Change user group or not
	$duplicate = $this->m_booking->duplicate($this->table, array('uid' => $id, 'gro_id !=' => $gro_id), '', '');	
	if ($duplicate)
	{
		// Check Old Permission
		$permission_dup = $this->m_booking->duplicate('controllers_permission',array('user_id' => $id));		
		if ($permission_dup)
		{
			if(! $this->m_booking->delete('controllers_permission', array('user_id' => $id)))
			{
				$error++;
				$error_msg	.= '<p>-<em>[#3.1]</em><p/>';					
			}
		}
			
		// Get List Permission User By ID	
		$wheres_con	= array('id_group' => $gro_id);
		$controller	= $this->m_booking->get_select_by_fields('user_groups', $wheres_con, 'controller_id', 'controller_id', '');
		
		// Insert (Set Controller Permission)
		$data_permission	= array(								
									'user_id'		=> $id,
									'controller_id'	=> $controller[0]['controller_id'],
									);
		if(! $this->m_booking->insert('controllers_permission', $data_permission))
		{
			$error++;
			$error_msg	.= '<p>-<em>[#1.1]</em><p/>';					
		}		
	}
			
	if($this->input->post('password') != '' && $this->input->post('con_password')){
		$where = array('uid' => $id);
		$uuid = uniqid('', true);
		$hash = $this->m_user->hashSSHA($this->input->post('password'));
		$encrypted_password = $hash["encrypted"]; // encrypted password
		$salt = $hash["salt"]; // salt
		
		$data_arr = array(
							'gro_id'		=> $gro_id,
							'unique_id'		=> $uuid, 
							'name'			=> $this->input->post('user_name'), 
							'email' 		=> $this->input->post('email_address'), 
							'encrypted_password' 	=> $encrypted_password, 
							'salt' 			=> $salt, 
							'updated_date' 	=> date('Y-m-d H:i:s')  
						 );
		 $data['table']	=	$this->m_booking->update_field('users', $data_arr, $where);
		 redirect(base_url().'admin/c_user/get_list', 'location', 302);
	}else{
		$where = array('uid' => $id);
		$uuid = uniqid('', true);
		$hash = $this->m_user->hashSSHA($this->input->post('password'));
		$encrypted_password = $hash["encrypted"]; // encrypted password
		$salt = $hash["salt"]; // salt
		
		$data_arr = array(
							'gro_id'		=> $gro_id,
							'unique_id'		=> $uuid, 
							'name'			=> $this->input->post('user_name'), 
							'email' 		=> $this->input->post('email_address'), 
							'updated_date' 	=> date('Y-m-d H:i:s')  
						 );
		 $data['table']	=	$this->m_booking->update_field('users', $data_arr, $where);
		 redirect(base_url().'admin/c_user/get_list', 'location', 302);
	}
 }
}

?>
